package org.budo.jdbc.driver.resultset.impl;

import java.sql.ResultSetMetaData;
import java.util.Map;

import org.budo.jdbc.driver.BudoJdbcResultSetMetaData;

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 多列多行结果集
 * 
 * @author lmw
 */
@Slf4j
@NoArgsConstructor
public class MapArrayResultSet extends ObjectArrayResultSet<Map<?, ?>> {
    public MapArrayResultSet(String[] columnNames, Map<?, ?>[] valueArray) {
        this(new BudoJdbcResultSetMetaData(columnNames), valueArray);
    }

    public MapArrayResultSet(ResultSetMetaData metaData, Object[] valueArray) {
        this.setMetaData(metaData);
        this.setValueArray((Map<?, ?>[]) valueArray);
    }

    @Override
    protected Object getColumnValueRaw(String columnLabel) {
        int rowNumber = this.getRowNumber();
        Map<?, ?> map = (Map<?, ?>) this.getValueArray()[rowNumber];
        if (null == map) {
            log.warn("#33 map=" + map + ", rowNumber=" + rowNumber + ", columnLabel=" + columnLabel + ", valueArray.length=" + this.getValueArray().length);
            return null;
        }

        Object value = map.get(columnLabel);
        if (null == value) {
            this.setLastValueWasNull(true);
        }

        return value;
    }
}
